<template>
  <div class="channel-container">
    <div class="my-channel">
      <!-- 我的频道 -->
      <van-cell :border="false">
        <div slot="title" class="cell-title">我的频道</div>
        <van-button plain type="info" round class="edit-btn" @click="isEditBtn = !isEditBtn">{{isEditBtn ? '完成' : '编辑'}}</van-button>
      </van-cell>
      <van-grid :gutter="10">
        <van-grid-item v-for="(item, index) in channels" :key="item.id" class="grid-item" @click="changeGrid(item, index)">
          <span slot="text" class="text" :class="{active: active === index}">{{item.name}}</span>
          <van-icon name="clear" slot="icon" v-show="isEditBtn && index !== 0" />
        </van-grid-item>
      </van-grid>
    </div>
    <!-- 频道推荐 -->
    <div class="recommend-channel">
      <!-- 头部 -->
      <van-cell :border="false">
        <div slot="title" class="cell-title">频道推荐</div>
      </van-cell>
      <!-- 宫格 -->
      <van-grid :gutter="10">
        <van-grid-item v-for="(item, index) in recommendList" :key="index" icon="plus" :text="item.name" class="grid-item" @click="addChannel(item)"/>
      </van-grid>
    </div>
  </div>
</template>

<script>
import { getAllChannel, addChannel, deleteChannel } from '@/api/channel'
import { mapState } from 'vuex'
import { setUser } from '@/utils/storage'
export default {
  data() {
    return {
      allChannel: [], // 频道列表数据
      isEditBtn: false // 是否显示编辑按钮
    }
  },
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  created() {
    this.loadAllChanel()
  },
  mounted() {},
  methods: {
    // 获取全部频道列表
    async loadAllChanel() {
      try {
        const { data } = await getAllChannel()
        this.allChannel = data.data.channels
      } catch (err) {
        this.$toast.fail('获取频道列表失败')
      }
    },
    // 添加用户频道列表
    async addChannel(item) {
      this.channels.push(item)
      if (this.user) {
        // 以登录
        try {
          await addChannel({
            id: item.id,
            seq: this.channels.length
          })
        } catch (err) {
          this.$toast.fail('添加到我的频道失败')
        }
      } else {
        // 未登录
        setUser('channel', this.channels)
      }
    },
    // 跳转或删除我的频道
    async changeGrid(item, index) {
      if (this.isEditBtn) {
        // 删除
        if (index !== 0) {
          this.channels.splice(index, 1)
          if (index <= this.active) {
            this.$emit('target-page', this.active - 1, true)
          }
          if (this.user) {
            // 以登录
            try {
              await deleteChannel(item.id)
            } catch (err) {
              this.$toast.fail('删除我的频道失败')
            }
          } else {
            // 未登录
            setUser('channel', this.channels)
          }
        }
      } else {
        // 跳转
        this.$emit('target-page', index, false)
      }
    }
  },
  computed: {
    // 获取频道列表数据(计算属性的优点就是数据发生变化，会自动重新执行)
    recommendList() {
      return this.allChannel.filter(item => {
        // find方法返回找到符合条件的第一项
        return !this.channels.find(items => {
          return items.id === item.id
        })
      })
    },
    ...mapState(['user'])
  }
}
</script>

<style scoped lang="less">
.channel-container {
  padding: 82px 0 0;
  .cell-title {
    font-size: 32px;
    color: #333;
  }
  .edit-btn {
    width: 120px;
    height: 48px;
    font-size: 26px;
    color: #f86363;
    border: 1px solid #f86363;
  }
  /deep/.grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      background-color: #f4f5f6;
      white-space: nowrap;
      .van-grid-item__text, .text {
        font-size: 28px;
        color: #222;
      }
      .active {
        color: red;
      }
    }
  }
  /deep/.van-grid-item__text {
    margin-top: 0;
  }
  /deep/.my-channel {
    .van-icon-clear {
      position: absolute;
      top: -10px;
      right: -10px;
      font-size: 36px;
      color: #cacaca;
      z-index: 2;
    }
  }
  /deep/.recommend-channel {
    .van-grid-item__content {
      flex-direction: row;
      .van-icon-plus {
        font-size: 24px;
        color: #222;
      }
    }
  }
  /deep/ .van-grid-item__icon-wrapper {
    position: unset;
  }
}
</style>
